package com.xing.adapter;

import java.util.List;

import com.google.gson.Gson;
import com.xing.controller.CommonController;
import com.xing.controller.DBController;
import com.xing.model.HsdDataResearch;
import com.xing.model.HsdModelSurvey;
import com.xing.model.HsdUploadDetail;
import com.xing.model.SuperObject;
import com.xing.model.UploadDetail;
import com.xing.my_interface.CallBack;
import com.xing.questionnairesurvey.R;
import com.xing.util.ViewUtil;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class UploadListAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private List<HsdModelSurvey> mList;
	private Context mContext;
	private FragmentManager fragmentManager;
	
	
	private CallBack myCallBack;
	
	public UploadListAdapter(Context context,List<HsdModelSurvey> list,FragmentManager frmg,CallBack callBack){
		try {
			this.inflater = LayoutInflater.from(context.getApplicationContext());
			this.mList = list;
			this.mContext = context;
			this.fragmentManager = frmg;
			this.myCallBack = callBack;
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("UploadListAdapter() excep"+e.getMessage());
		}
		
	}
	
	public void setItemList(List<HsdModelSurvey> itemList) {
		this.mList = itemList;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		if(mList != null){
			return mList.size();
		}
		return 0;
	}

	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		if(mList != null){
			return mList.get(arg0);
		}
		return null;
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		if(mList != null){
			return position;
		}
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final HsdModelSurvey object  = mList.get(position);
//			ViewHolder holder;
			if(convertView == null){
//				holder = new ViewHolder();
				convertView = inflater.inflate(R.layout.upload_list_item, null);
//				holder.titleTextView = (TextView) convertView.findViewById(R.id.questionnaire_name);
//				holder.hasUploadTextView = (TextView) convertView.findViewById(R.id.has_done_number);
//				holder.notUploadTextView = (TextView) convertView.findViewById(R.id.not_done_number);
				
				
//				convertView.setTag(holder);
			}else
			{
//				holder = (ViewHolder) convertView.getTag();
			}
			
		
		TextView titleTextView = (TextView) convertView.findViewById(R.id.questionnaire_name);
		TextView hasUploadTextView = (TextView) convertView.findViewById(R.id.has_uploaded_number);
		TextView notUploadTextView = (TextView) convertView.findViewById(R.id.not_upload_number);
		
		
		titleTextView.setText(object.getName());
		DBController.getInstance().getResearchUpStatus(mContext,object.getSurveyuuid(),hasUploadTextView,notUploadTextView,uploadDetailHandler);
		
		convertView.findViewById(R.id.start_upload).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
					DBController.getInstance().getHsdDataResearchNotUploadList(mContext, object.getSurveyuuid(), getHsdDataResearchHandler);
				
				
			}
		});
		return convertView;
	}
	
	
//	static class ViewHolder{
//		TextView titleTextView;
//		TextView hasUploadTextView;
//		TextView notUploadTextView;
//	}
	
	private Handler getHsdDataResearchHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			
			List<HsdDataResearch> list =(List<HsdDataResearch>) msg.obj;
			if (list != null && list.size() > 0) {
				for (HsdDataResearch hsdDataResearch : list) {
					hsdDataResearch.setResearchuuid(hsdDataResearch.getResearchuuid());
					hsdDataResearch.setSurveyuuid(hsdDataResearch.getSurveyuuid());
					hsdDataResearch.setName(hsdDataResearch.getName());
					hsdDataResearch.setSex(hsdDataResearch.getSex());
					hsdDataResearch.setAge(hsdDataResearch.getAge());
					hsdDataResearch.setTelephone(hsdDataResearch.getTelephone());
					hsdDataResearch.setMobilephone(hsdDataResearch.getMobilephone());
					hsdDataResearch.setEmail(hsdDataResearch.getEmail());
					hsdDataResearch.setDocumenttype(hsdDataResearch.getDocumenttype());
					hsdDataResearch.setIdnumber(hsdDataResearch.getIdnumber());
					hsdDataResearch.setResearchaccount(hsdDataResearch.getResearchaccount());
					hsdDataResearch.setResearchname(hsdDataResearch.getResearchname());
					hsdDataResearch.setCreatetime(hsdDataResearch.getCreatetime());
					hsdDataResearch.setModifytime(hsdDataResearch.getModifytime());
					
					DBController.getInstance().getHsdUploadDetail(mContext, hsdDataResearch, hsdDataResearch.getSurveyuuid(), hsdDataResearch.getResearchuuid(), getHsdUploadDetailHandler);
				}
			}else {
				ViewUtil.showToast(mContext, "此问卷已经全部上传成功或者没有可以上传的问卷");
			}
			
		}
	};
	
	private Handler getHsdUploadDetailHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			HsdUploadDetail hsdUploadDetail =  (HsdUploadDetail) msg.obj;
			Gson gson1 = new Gson();
			String hsdResearch = gson1.toJson(hsdUploadDetail.getHsdDataResearch());
			Gson gson2 = new Gson();
			String resultListString = gson2.toJson(hsdUploadDetail.getResultsList());
			System.out.println("hsdResearch--"+hsdResearch);
			System.out.println("resultListString--"+resultListString);
			
			CommonController.getInstance().upLoadHsdResult(mContext, hsdResearch, resultListString, uploadToServerHandler);
		
		}
	};
	

	private Handler uploadToServerHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			Bundle bundle = msg.getData();
			HsdDataResearch hsdDataResearch = (HsdDataResearch) bundle.getSerializable("hsd_data_research");
			SuperObject superObject = (SuperObject) bundle.getSerializable("result");
			if (superObject == null) {
				ViewUtil.showToast(mContext, "网络异常");
			}else {
				if (superObject.status == 1) {
					DBController.getInstance().updateHsdDataResearchUpStatus(mContext, hsdDataResearch.getSurveyuuid(), hsdDataResearch.getResearchuuid(), uploadHsdDataResearchUpStatus);
				}else {
					ViewUtil.showToast(mContext, "上传失败");
				}
			}
			
		}
	};
	
	private Handler uploadDetailHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			UploadDetail uploadDetail = (UploadDetail) msg.obj;
			if (uploadDetail != null) {
				((TextView)uploadDetail.getHasUploadedView()).setText(String.valueOf(uploadDetail.getHasUploaded()));
				((TextView)uploadDetail.getNotuploadView()).setText(String.valueOf(uploadDetail.getNotUpload()));
			}
			
		}
	};
	
	private Handler uploadHsdDataResearchUpStatus = new Handler(){
		public void handleMessage(android.os.Message msg) {
			boolean isUpdate = (Boolean) msg.obj;
			System.out.println("isupdate-------"+isUpdate);
			if (isUpdate) {
				ViewUtil.showToast(mContext, "上传后更新本地状态成功");
				myCallBack.fresh();
			}else {
				ViewUtil.showToast(mContext, "更新本地上传状态失败");
			}
			
		}
	};
	

}
